//
//  FeedbackViewController.m
//  SouthChina
//
//  Created by Mr.Y on 14/11/5.
//  Copyright (c) 2014年 科创. All rights reserved.
//

#import "FeedbackViewController.h"
#import "DataDictDao.h"
@interface FeedbackViewController ()<UITextViewDelegate>
@property (retain, nonatomic)UITextView *feedbackTextView;
@end

@implementation FeedbackViewController
@synthesize feedbackTextView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        

    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addTitleBarWithBackBtn:@"意见反馈" needReturn:YES];
    UIImageView * backgroundImg = [[UIImageView alloc] initWithFrame:CGRectMake(5, TITLE_BAR_HEGIHT+(YNEW)+5, SCREEN_WIDTH-10, 180)];
    backgroundImg.image = [UIImage imageNamed:@"feedbackbackground.png"];
    [self.view addSubview:backgroundImg];
    feedbackTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, TITLE_BAR_HEGIHT+(YNEW)+10, SCREEN_WIDTH-20, 170)];
    feedbackTextView.font = [UIFont systemFontOfSize:16];
    feedbackTextView.backgroundColor = [UIColor clearColor];
    feedbackTextView.returnKeyType = UIReturnKeyDefault;//返回键的类型
    feedbackTextView.keyboardType = UIKeyboardTypeDefault;//键盘类型
    
    [self.view addSubview:feedbackTextView];
    //提交
    UIImage *img=[UIImage imageNamed:@"blue_btn_normal.png"];
    img=[img stretchableImageWithLeftCapWidth:20 topCapHeight:20];
    UIButton * submitBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, feedbackTextView.frame.origin.y + feedbackTextView.frame.size.height + 25, (SCREEN_WIDTH)-20, 35)];
    [submitBtn addTarget:self action:@selector(submit:) forControlEvents:UIControlEventTouchUpInside];
    [submitBtn setTitle:@"提交" forState:UIControlStateNormal];
    [submitBtn setBackgroundImage:img forState:UIControlStateNormal];
    [self.view addSubview:submitBtn];
    
    feedbackTextView.delegate = self;
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 *	@brief	提交反馈意见后，如果没有关注意见反馈公众号，后台会重新关注，所以还需用重新刷新应用
 *
 *	@param 	sender 	<#sender description#>
 */
-(void)submit:(id)sender

{
    [feedbackTextView resignFirstResponder];
    
    if ([feedbackTextView.text isEqualToString:@""]) {
        [self showAlerHUD:@"请输入内容！"];
        return;
    }
     NSDictionary *map = @{@"content":feedbackTextView.text};
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [DE serverCall:@"submitFeedBack" params:map callback:^(BOOL success, int code, NSString *desc, NSDictionary *data,NSDictionary*param) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if(success)
        {
            [self showAlerHUD:@"提交成功！"];
            [self checkPublicToUpdate];
        }
        else{
            [self showAlerHUD:@"提交失败！"];
        }
    }];

}
-(void)checkPublicToUpdate
{
    NSDictionary *map = @{@"user_id":DE.userId};
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [DE serverCall:@"queryApps" params:map callback:^(BOOL success, int code, NSString *desc, NSDictionary *data,NSDictionary*param) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if(success)
        {
            [PublicAccountDao updataDBwithSQLiteStatement:[data objectForKey:@"apps"]];
            [DE setGlobalVar:LAST_REFRESH_PUBLIC_TIME value:[[NSDate date] stringFromDateDB]];
            [[NSNotificationCenter defaultCenter] postNotificationName:PUBLIC_ATTENTION_BROADCAST object:nil];
            //获取应用分类字典表
            NSArray *appTypesArray = [data objectForKey:@"dict"];

            [DataDictDao updataDataDictWithType:appTypesArray dicttypeId:@"6"];
        }
        else{
            [self showAlerHUD:@"提交失败！"];
        }
    }];
    

}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

@end
